import * as feishuService from '@/api/service/FeishuService'

import DateUtil from '@/framework/utils/date-util'


/**
 * 表格列表
 */
export function findList(queryBody?:any): Promise<any> {
  const tableId = globalThis.tableConfig.selection
  return feishuService.appTableRecords(tableId, queryBody, 100)
}

/**
 * 新增记录
 * @param record
 */
export function addRecord(record:any):Promise<any>{
  const tableId = globalThis.tableConfig.selection
  const fields = {
    "一级类目": [
        record['一级类目']
    ],
    "二级类目": [
        record['二级类目']
    ],
    "日期": DateUtil.toDate(record['日期']).getTime(),
    "产品链接": {
        "link": record['产品链接'],
        "text": record['产品链接'],
        "type": "url"
    },
    "产品名称": record['产品名称'],
    "商家名称": record['商家名称'],
    "佣金": record['佣金'] / 100

  }

  return feishuService.addRecord(tableId, fields)
}

/**
 * 更新记录
 * @param record
 */
export function updateRecord(recordId: string, record: any):Promise<any>{
  const tableId = globalThis.tableConfig.selection
  const fields = {
    "一级类目": [
        record['一级类目']
    ],
    "二级类目": [
        record['二级类目']
    ],
    "日期": DateUtil.toDate(record['日期']).getTime(),
    "产品链接": {
        "link": record['产品链接'],
        "text": record['产品链接'],
        "type": "url"
    },
    "产品名称": record['产品名称'],
    "商家名称": record['商家名称'],
    "佣金": record['佣金'] / 100,

  }

  return feishuService.updateRecord(tableId, recordId, fields)
}


/**
 * 删除记录
 * @param recordId
 */
export function removeRecord(recordId:string):Promise<any>{
  const tableId = globalThis.tableConfig.selection
  return feishuService.removeRecord(tableId, recordId);
}
